<template>
  <view class="device-detail" v-if="initFinish">
    <view class="form-content">
      <form ref="formData">
        <table>
          <tr>
            <td class="key">设备编号</td>
            <td class="value">
              <view>
                <p>{{formData.eqCode}}</p>
              </view>
            </td>
          </tr>
          <tr v-for="item in formList" :key="item.code">
            <td class="key">{{item.name}}</td>
            <td class="value">
              <view>
                <p>{{formData[item.code]}}</p>
              </view>
            </td>
          </tr>
        </table>
      </form>
    </view>
    <view class="btn-content" v-if="$store.state.user.userState * 1 === 2">
      <button @click="addNew" :disabled="!ableSubmit" class="cu-btn block bg-blue lg">维修申请</button>
      <button @click="selfCheck" v-if="deviceInfo.selfInspect > 0" class="cu-btn block lg">自检</button>
    </view>
  </view>
</template>

<script>
  import {
    getEquipmentInfo,
    createOrder,
    deviceSelfCheck,
  } from '../../api/device/device.js';
  import {
    showConfirm,
  } from '@/utils/common';
  
  // eqName: '', //  -名称
  // eqStylenumber: '', //  -设备型号
  // eqNumber: '', //  -设备序列号
  // eqProductiondate: '', //  -生产日期
  // eqStartdate: '', //  -启用日期
  // eqServicelife: '', //  -使用年限
  // eqOriginprice: '', //  -原价值
  // eqRegisternumber: '', //  -注册证号
  // eqLicensenumber: '', //  -生产许可证号
  // eqMemo: '', //  -备注说明
  // officeName: '', //  -科室
  const formList = [
    {
      name: '科室',
      code: 'officeName',
    },
    {
      name: '设备名称',
      code: 'eqName',
    },
    {
      name: '设备型号',
      code: 'eqStylenumber',
    },
    {
      name: '设备序列号',
      code: 'eqNumber',
    },
    {
      name: '生产日期',
      code: 'eqProductiondate',
    },
    {
      name: '启用日期',
      code: 'eqStartdate',
    },
    {
      name: '使用年限',
      code: 'eqServicelife',
    },
    {
      name: '原价值',
      code: 'eqOriginprice',
    },
    {
      name: '注册证号',
      code: 'eqRegisternumber',
    },
    {
      name: '生产许可证号',
      code: 'eqLicensenumber',
    },
    {
      name: '备注说明',
      code: 'eqMemo',
    },
  ];
  export default {
    data() {
      return {
        formList,
        fileList: [],
        formData: {
          eqCode: '', // -编号 T00000005
        },
        initFinish: false,
        ableSubmit: true,
        deviceInfo: {},
      };
    },
    created() {
    },
    onShow() {
      const pageList = getCurrentPages();
      const param = pageList[ pageList.length - 1 ].options || {};
      this.formData = {
        eqCode: param.eqCode,
      };
      this.init();
    },
    methods: {
      init() {
        this.setFormData();
        this.getData();
      },
      setFormData() {
        const {
          formData,
        } = this;
        formList.forEach((val) => {
          formData[ val.code ] = '';
        });
        this.formData = formData;
      },
      async selfCheck() {
        const { deviceInfo } = this;
        if (deviceInfo.selfInspectComplete > 0) {
          uni.showModal({
            title: '提示',
            content: '今日自检已完成',
            confirmText: '确定',
            showCancel: false,
          });
          return;
        }
        const confirm = await showConfirm('是否完成自检？').catch(() => {
        });
        if (confirm.confirm) {
          const res = await deviceSelfCheck({
            eqCode: this.formData.eqCode,
          }).catch(() => {
            uni.showModal({
              title: '提示',
              content: '自检失败',
              confirmText: '确定',
              showCancel: false,
            });
          });
          if (res && res.code === 200) {
            uni.showModal({
              title: '提示',
              content: '自检已完成',
              confirmText: '确定',
              showCancel: false,
            });
            this.getData();
          }
        }
      },
      async addNew() {
        const confirm = await showConfirm('确定发起维修申请？').catch(() => {
        });
        if (!confirm.confirm) {
          return;
        }
        
        const formData = {
          eqName: this.formData.eqName,
          eqCode: this.formData.eqCode,
        };
        this.ableSubmit = false;
        const res = await createOrder(formData).catch(() => {
          this.ableSubmit = true;
        });
        this.ableSubmit = true;
        if (res && res.code === 200) {
          const pageList = getCurrentPages();
          uni.showToast({
            title: '提交成功',
            icon: 'success',
          });
          if (pageList[ pageList.length - 2 ] && pageList[ pageList.length - 2 ].route === 'pages/index/index') {
            uni.navigateBack({
              delta: 1,
            });
          } else {
            uni.switchTab({
              url: '/pages/index/index',
            });
          }
          return;
        }
        uni.showToast({
          title: res.msg || '提交失败',
          icon: 'error',
        });
        // uni.navigateTo({
        //   url: `/pages/order/add-new/addNew?eqCode=${this.formData.eqCode}`,
        // });
      },
      async getData() {
        const res = await getEquipmentInfo({
          eqCode: this.formData.eqCode,
        });
        if (res.code === 200) {
          res.data = res.data || {};
          this.deviceInfo = res.data;
          this.formData = {
            ...this.formData,
            ...res.data,
          };
          this.initFinish = true;
          return;
        }
        uni.showModal({
          title: '提示',
          content: res.msg || '查询失败',
          showCancel: false,
          success(res) {
            if (res.confirm) {
              uni.navigateBack({
                delta: 1,
              });
            }
          },
        });
        console.log(res);
      },
    },
  };
</script>

<style lang="scss" scoped>
  .device-detail {
    
    &,
    * {
      box-sizing: border-box;
    }
    
    height: 100vh;
    background-color: white;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    
    .form-content {
      flex: 1;
      padding: 10px;
      overflow: auto;
      
      table {
        border-collapse: collapse;
        
        tr {
          td {
            padding: 10px;
            border-bottom: 1px solid #ddd;
            
            &.key {
              white-space: nowrap;
            }
            
            &.value {
              width: 100%;
              
              > view {
                // border: 1px solid #ddd;
                border-radius: 3px;
                padding: 5px 10px;
                min-height: 38px;
                display: flex;
                align-items: center;
                
                p {
                  font-size: 14px;
                  line-height: 1.5;
                }
              }
            }
            
          }
        }
      }
    }
    
    .btn-content {
      padding: 10px;
      display: flex;
      button {
        flex: 1;
        + button {
          margin-left: 15px;
        }
      }
    }
  }
</style>
